<template>
	<view class="box">
		<view class="tips" v-if="type==1">
			温馨提示：积分兑换，{{chooseitem.select.quota||''}}{{chooseitem.select.function_name.quota_unit||''}}可兑换{{chooseitem.select.term||''}}天家味{{chooseitem.grade_name||''}}
		</view>
		<view class="tips" v-if="type==2">
			温馨提示：100个食材可兑换10积分
		</view>
		<view class="box1 tr">
			<view class="num bold" v-if="type==1">{{userinfo.integral||''}}</view>
			<view class="num bold" v-if="type==2">{{banquet_sum||''}}</view>
			<view class="f24" v-if="type==1">可使用积分</view>
			<view class="f24" v-if="type==2">当前食材</view>
		</view>
		<!--  -->
		<view class="box2" v-if="type==1">
			<view class="title f24">请选择兑换积分</view>
			<picker mode="selector" :range="arr2" range-key="name" @change="getchange">
				<view>
					<input type="number" disabled :value="chooseitem.select.quota" placeholder="请选择兑换积分">
				</view>
			</picker>

		</view>
		<view class="box2" v-if="type==2">
			<view class="title f24">请输入兑换食材</view>
			<view>
				<input type="number" @input="getInterg" v-model="num" :max="total" placeholder="请输入兑换食材">
			</view>
		</view>
		<view class="box3" @click="goDuihuan" v-if="type==1">立即兑换</view>
		<view class="box3" @click="goDuihuanshicai" v-if="type==2">立即兑换</view>
	</view>
</template>

<script>
	import {
		getGrade
	} from '@/api/grade.js'
	export default {
		data() {
			return {
				type: 1,
				total: 100,
				banquet_sum: 0,
				num: '', //
				index: 0,
				arr2: [],
				chooseitem: '',
				userinfo: '',
				bid: ''
			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type;
			}
		},
		onShow() {
			if (this.type == 1) {
				this.getlist()
			}
			if (this.type == 2) {
				this.getinfo2()
			}
			this.getuserinfo()
		},
		methods: {
			getinfo2() {
				this.$request("user/banquet/lst", {}, 'GET').then(res => {
					this.banquet_sum = res.data.target.banquet_sum
					this.bid = res.data.target.id
				})

			},
			getuserinfo() {
				this.$request('user', {}, 'GET').then(res => {
					this.userinfo = res.data
				})
			},
			getchange(e) {
				this.index = e.detail.value
				this.chooseitem = this.arr2[this.index]
			},
			getlist() {
				let arr2 = []
				getGrade().then(res => {
					res.data.forEach(item => {
						item.condition.forEach(item2 => {
							if (item2.function_name.function_name == 'integral_exchange') {
								item.select = item2
								item.name = item2.quota + item2.function_name.quota_unit + item2
									.term + '天'
								arr2.push(item)
							}
						})
					})
					this.arr2 = arr2
					this.chooseitem = arr2[this.index]
				})
			},
			// 检测积分
			getInterg(e) {
				// console.log(e);

			},
			// 兑换食材
			goDuihuanshicai() {
				if (!this.num) {
					this.$showToast('请输入兑换食材')
					return false;
				}

				let data = {
					id: this.bid,
					num: this.num
				}
				this.$request('user/banquet/integral', data).then(res => {
					if (res.status == 200) {
						uni.hideLoading()

						uni.navigateTo({
							url: '/pages/state/success/success?type=10'
						})

					}
				})

			},
			// 立即兑换
			goDuihuan() {
				// console.log(this.num);
				if (this.num <= 0) {
					this.$showToast('兑换积分应大于0！')
					this.num = 1;
					return false;
				}
				if (this.num > this.total) {
					this.num = this.total;
					this.$showToast('超出最大兑换积分！')
					return false;
				}
				uni.showLoading({
					mask: true
				})
				this.$request('grade/create_pay_order', {
					unique: this.chooseitem.select.unique
				}).then(res => {
					if (res.status == 200) {
						uni.hideLoading()
						if (this.type == 1) {
							uni.navigateTo({
								url: '/pages/state/success/success?type=2'
							})
						} else if (this.type == 2) {
							uni.navigateTo({
								url: '/pages/state/success/success?type=10'
							})
						}
					}
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #F5F5F5;
	}
</style>

<style lang="scss" scoped>
	.box {
		.tips {
			width: 750rpx;
			height: 80rpx;
			padding: 0 30rpx;
			line-height: 80rpx;
			font-size: 26rpx;
			color: #F83535;
			background: #FFF3F3;
			box-sizing: border-box;
		}

		.box1 {
			width: 750rpx;
			height: 302rpx;
			background: linear-gradient(179deg, #2BCCA5, #22B591);
			overflow: hidden;
			color: #fff;
			box-sizing: border-box;

			.num {
				margin-top: 50rpx;
				font-size: 80rpx;
			}
		}

		.box2 {
			margin: -70rpx auto 0;
			padding: 0 30rpx;
			width: 690rpx;
			height: 300rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-sizing: border-box;
			overflow: hidden;

			.title {
				margin: 100rpx auto 45rpx;
			}

			input {
				font-size: 28rpx;
			}
		}

		.box3 {
			margin: 30rpx auto;
			width: 690rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: #fff;
			font-size: 32rpx;
			background: linear-gradient(64deg, #2BCCA5, #22B591);
			box-shadow: 0rpx 4rpx 6rpx 0rpx rgba(20, 187, 146, 0.4);
			border-radius: 10rpx;
		}
	}
</style>
